/**
 * @author LKQ
 * @date 2021/12/28 10:42
 * @description 优化，使用双指针一个指向左边界，一个指向右边界。
 */
public class Solution2 {
    public static void main(String[] args) {
        Solution2 solution2 = new Solution2();
        int[] nums = {2, 7, 11, 15};
        solution2.twoSum(nums, 9);
    }

    public int[] twoSum(int[] nums, int target) {
        int n = nums.length, left = 0, right = n - 1;
        while (left < right) {
            int temp = nums[left] + nums[right];
            if (temp < target) {
                left++;
            }else if (temp > target) {
                right--;
            }else {
                break;
            }
        }
        return new int[] {left+1, right+1};
    }
}
